package com.zs.studentmanager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.studentmanager.common.ResponseCodeEnum;
import com.zs.studentmanager.entity.domain.Course;
import com.zs.studentmanager.entity.domain.Teacher;
import com.zs.studentmanager.entity.vo.PageVo;
import com.zs.studentmanager.entity.vo.TeacherVo;
import com.zs.studentmanager.exception.CustomException;
import com.zs.studentmanager.mapper.CourseMapper;
import com.zs.studentmanager.mapper.TeacherMapper;
import com.zs.studentmanager.service.TeacherService;
import com.zs.studentmanager.utils.CommonUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author 周小白
* @description 针对表【s_teacher(教师信息表)】的数据库操作Service实现
* @createDate 2024-01-19 19:58:37
*/
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
    implements TeacherService{

    private final TeacherMapper teacherMapper;

    private final CourseMapper courseMapper;

    public TeacherServiceImpl(TeacherMapper teacherMapper, CourseMapper courseMapper){
        this.teacherMapper = teacherMapper;
        this.courseMapper = courseMapper;
    }




    /**
     * 获取教师列表
     * @param pageNum 当前页
     * @param pageSize 每页显示条数
     * @param searchValue 搜索值
     * @return 教师列表
     */
    @Override
    public PageVo<TeacherVo> getTeacherList(Integer pageNum, Integer pageSize, String searchValue) {
        if (pageNum == null || pageSize == null || pageNum <= 0 || pageSize <= 0) {
            throw new CustomException("参数错误");
        }

        PageVo<TeacherVo> pageVo = new PageVo<>();

        // 获取总条数
        Long total = teacherMapper.selectCount(null);
        pageVo.setTotal(total);

        // 查询
        pageNum = (pageNum - 1) * pageSize;
        List<TeacherVo> teacherVoList = teacherMapper.getTeacherList(pageNum, pageSize, searchValue);
        if (teacherVoList == null) {
            throw new CustomException(ResponseCodeEnum.SYSTEM_ERROR);
        }
        pageVo.setList(teacherVoList);

        return pageVo;
    }




    /**
     * 添加教师
     * @param teacherVo 教师信息
     * @return 是否添加成功
     */
    @Override
    public boolean addTeacher(TeacherVo teacherVo) {
        Teacher teacher = new Teacher();
        // 复制属性
        BeanUtil.copyProperties(teacherVo, teacher);
        // 获取课程id
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Course::getCourseName, teacherVo.getCourseName());
        Course course = courseMapper.selectOne(queryWrapper);
        if (course == null) {
            throw new CustomException("课程不存在");
        }
        // 设置课程id
        teacher.setCourseId(course.getId());
        // 设置教师信息
        teacher.setCreateTime(LocalDateTime.now());
        teacher.setCreateBy(CommonUtils.getCurrentUserId());

        // 添加
        int result = teacherMapper.insert(teacher);
        if (result != 1) {
            throw new CustomException(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return true;
    }



    /**
     * 获取教师详情
     * @param id 教师id
     * @return 教师详情
     */
    @Override
    public TeacherVo getTeacherDetails(Long id) {
        if (id == null) {
            throw new CustomException(ResponseCodeEnum.NO_PARAM);
        }

        TeacherVo teacherVo = teacherMapper.getTeacherDetails(id);
        if (teacherVo == null) {
            throw new CustomException(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return teacherVo;
    }


    /**
     * 修改教师信息
     * @param teacherVo 教师信息
     * @return 是否修改成功
     */
    @Override
    public boolean updateTeacher(TeacherVo teacherVo) {
        if (teacherVo == null) {
            throw new CustomException(ResponseCodeEnum.NO_PARAM);
        }

        Teacher teacher = new Teacher();
        // 复制属性
        BeanUtil.copyProperties(teacherVo, teacher);

        // 修改
        int result = teacherMapper.updateById(teacher);
        if (result != 1) {
            throw new CustomException(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return true;
    }
}




